package deng.camel.examples;

import java.util.concurrent.CountDownLatch;

import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class ListComponentDemo extends RouteBuilder {
	
	private static Log logger = LogFactory.getLog(ListComponentDemo.class);
	
	public static void main(String[] args) {
		new ListComponentDemo().start();
	}
	
	private CountDownLatch latch = new CountDownLatch(1);
	private DefaultCamelContext camelContext;
	private Processor countDownLatchProcessor;
	
	public void start() {
		try {
			countDownLatchProcessor = new CountDownLatchProcessor();
			camelContext = new DefaultCamelContext();
			camelContext.addRoutes(this);
			camelContext.start();
			afterCamelStart();
			latch.await();
			camelContext.stop();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}		
	}

	private void afterCamelStart() {
		ProducerTemplate producer = camelContext.createProducerTemplate();
		producer.sendBody("direct:startRoute", "Hello");
	}

	@Override
	public void configure() throws Exception {
		from("direct:startRoute")
			.to("list:exchanges")  //????
			.process(countDownLatchProcessor);		
	}
	
	private class CountDownLatchProcessor implements Processor {
		@Override
		public void process(Exchange exchange) throws Exception {
			logger.trace("Count down latch");
			latch.countDown();
		}				
	}

}
